package com.bpz.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class CookieUtils {
	/**
	 *  * 设置cookie  
	 * 
	 * @param response
	 * @param name
	 *              cookie名字
	 * @param value
	 *            cookie值  
	 * @param maxAge
	 *            cookie生命周期  以秒为单位  
	 */
	public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) {
		Cookie cookie = new Cookie(name, value);
		cookie.setPath("/");
		if (maxAge > 0)
			cookie.setMaxAge(maxAge);
		response.addCookie(cookie);
	}

	/**
	 * 设置cookie,默认生命周期一周
	 * 
	 * @param response
	 * @param name
	 * @param value
	 */
	public static void addCookie(HttpServletResponse response, String name, String value) {
		addCookie(response, name, value, 60 * 60 * 24 * 7);
	}

	/**
	 * 根据名字获取cookie  
	 * 
	 * @param request
	 *             
	 * @param name
	 *            cookie名字  * @return  
	 */
	public static Cookie getCookieByName(HttpServletRequest request, String name) {
		Map<String, Cookie> cookieMap = ReadCookieMap(request);
		if (cookieMap.containsKey(name)) {
			Cookie cookie = (Cookie) cookieMap.get(name);
			return cookie;
		}
		return null;
	}
/**
 * 删除cookie
 * @param request
 * @param name
 * @return
 */
	public static Status removeCookieByName(HttpServletRequest request,HttpServletResponse response, String name) {
		Map<String, Cookie> cookieMap = ReadCookieMap(request);
		if (cookieMap.containsKey(name)) {
			Cookie cookie = cookieMap.get(name);
			cookie.setMaxAge(0);
			cookie.setPath("/");
			response.addCookie(cookie);
            try{
                response.flushBuffer();
            }catch (IOException ex)
            {

            }

            response.reset();
			return Status.SUCCESS;
		}
		return Status.SUCCESS;
	}

	/**
	 * 将cookie封装到Map里面  
	 * 
	 * @param request
	 * @return  
	 */
	private static Map<String, Cookie> ReadCookieMap(HttpServletRequest request) {
		Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
		Cookie[] cookies = request.getCookies();
		if (null != cookies) {
			for (Cookie cookie : cookies) {
				cookieMap.put(cookie.getName(), cookie);
			}
		}
		return cookieMap;
	}
}
